package com.flyco.modules.stm.controller;




import cn.hutool.core.bean.BeanUtil;
import com.flyco.modules.stm.service.DistrictService;
import com.flyco.modules.stm.vo.DistrictDto;
import com.flyco.modules.stm.vo.DistrictResult;
import com.flyco.modules.stm.vo.ResponseInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
@RequestMapping(path = "/f/district", method = RequestMethod.POST)
public class DistrictController {

    @Autowired
    private DistrictService districtService;

    @RequestMapping(path = "/provices")
    public ResponseInfo getAllProvice() {

        return new ResponseInfo(districtService.selectOneLevelDistrict());
    }

    @RequestMapping(path = "/counties/{cityId}")
    public ResponseInfo getCounties(@PathVariable Long cityId) {
        return new ResponseInfo(districtService.selectChildDistrictData(cityId));
    }

    @RequestMapping(path = "/cities/{proviceId}")
    public ResponseInfo getCities(@PathVariable Long proviceId) {
        return new ResponseInfo(districtService.selectChildDistrictData(proviceId));
    }

    @RequestMapping(path = "/towns/{cityId}")
    public ResponseInfo getTowns(@PathVariable Long cityId) {
        List<DistrictResult> districtResults = districtService.selectChildDistrictDataByParentId(cityId);


        return new ResponseInfo(districtResults);
    }

    @Deprecated
    @RequestMapping(path = "/getOneLevelDistrictData")
    public ResponseInfo getOneLevelDistrictData() {
        return new ResponseInfo(districtService.selectOneLevelDistrict());
    }

    @RequestMapping(path = "/detail/{districtId}")
    public ResponseInfo getOneDistrictById(@PathVariable Long districtId)  {
        return new ResponseInfo(districtService.selectOneById(districtId));
    }

    @RequestMapping(path = "/all")
    public ResponseInfo getAllDistrictData()  {
        return new ResponseInfo(districtService.selectAllDistrict());
    }

    @Deprecated
    @RequestMapping(path = "/getChildDistrictData")
    public ResponseInfo getChildDistrictData(Long parentDistrictId){
        if (StringUtils.isEmpty(parentDistrictId)) {
            throw new RuntimeException("不能null");
        }
        return new ResponseInfo(districtService.selectChildDistrictData(parentDistrictId));
    }

    @RequestMapping(path = "/delete/{id}")

    public ResponseInfo deleteDistrictById(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("");
        }
        return new ResponseInfo(districtService.deleteDistrictDataById(id));
    }

    @RequestMapping(path = "/create")

    public ResponseInfo createDistrict(DistrictDto districtDto, BindingResult result)  {

        if (result.hasErrors()) {
            // 初始化非法参数的提示信息。

            // 获取非法参数异常信息对象，并抛出异常。
            throw new RuntimeException("");
        }
        // 成功时，直接分发数据并调用业务逻辑方法，并返回响应信息对象。
        return new ResponseInfo(districtService.createDistrict(districtDto));
    }

    @RequestMapping(path = "/update")

    public ResponseInfo modifyDistrict( DistrictDto districtDto, BindingResult result)  {
        if (result.hasErrors()) {
            // 初始化非法参数的提示信息。

            throw new RuntimeException("");
        }
        if (districtDto.getId() == null) {
            throw new RuntimeException("");
        }
        // 成功时，直接分发数据并调用业务逻辑方法，并返回响应信息对象。
        return new ResponseInfo(districtService.modifyDistrict(districtDto));
    }
}
